package com.shinobi.timetracker;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.shinobi.timetracker.database.CurrentDateSettings;
import com.shinobi.timetracker.database.GlobalState;

//import com.androidplot.xy.BarFormatter;
//import com.androidplot.xy.BarRenderer;
//import com.androidplot.xy.SimpleXYSeries;
//import com.androidplot.xy.XYPlot;

/**
 * Created by IntelliJ IDEA. User: Semy Date: 09.05.11 Time: 20:31 To change
 * this template use File | Settings | File Templates.
 */
public class StatisticsActivity extends Activity {

	private GlobalState globalState;
	public CurrentDateSettings currentDate;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.globalState = (GlobalState) getApplication();
		this.currentDate = this.globalState.getCurrentDate();
		
		setContentView(R.layout.stats);
		InitializeSpinner();
//		Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
//
//		ArrayList<String> tags = globalState.GetTags(this);
//		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tags);
//		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		tagComboBox.setAdapter(adapter);
//
//        String defaultTag  = globalState.CurrentTag();
//        int position = adapter.getPosition(defaultTag);
//        
//        if (position < 0) {
//        	position = 0;
//        	tagComboBox.setSelection(position);
//        	globalState.SetCurrentTag((String)tagComboBox.getSelectedItem());
//        } else {
//        	tagComboBox.setSelection(position);
//        }
//		
//		tagComboBox.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> parentView,
//					View selectedItemView, int position, long id) {
//
//				GlobalState globalState = (GlobalState) getApplication();
//				Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
//				String tag = (String) tagComboBox.getSelectedItem();
//				globalState.SetCurrentTag(tag);
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				globalState.SetCurrentTag("");
//			}
//
//		});
		
		Button gotoTagsEdit = (Button) findViewById(R.id.tagsEditButton);
		gotoTagsEdit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				startActivity(new Intent(StatisticsActivity.this,
						TagsActivity.class));
			}
		});

		Button gotoChart = (Button) findViewById(R.id.ShowChartButton);
		gotoChart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				startActivity(new Intent(StatisticsActivity.this,
						ChartActivity.class));
			}
		});

		InitializeDataPickers();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		InitializeSpinner();
	}

	static final int DATE_DIALOG_ID1 = 0;
	static final int DATE_DIALOG_ID2 = 1;

	private TextView startDateText;
	private TextView stopDateText;

	public void InitializeDataPickers() {

		Button startDateButton = (Button) findViewById(R.id.StartDateButton);
		Button stopDateButton = (Button) findViewById(R.id.StopDateButton);
		startDateText = (TextView) findViewById(R.id.StartTextView);
		stopDateText = (TextView) findViewById(R.id.StopTextView);

		startDateButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				showDialog(DATE_DIALOG_ID1);
			}
		});

		stopDateButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				showDialog(DATE_DIALOG_ID2);
			}
		});

		updateDisplayStart();
		updateDisplayStop();
	}

	private DatePickerDialog.OnDateSetListener mDateSetListenerStart = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			currentDate.setStartYear(year);
			currentDate.setStartMonth(monthOfYear);
			currentDate.setStartDay(dayOfMonth);

			updateDisplayStart();
		}
	};

	private DatePickerDialog.OnDateSetListener mDateSetListenerStop = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			currentDate.setStopYear(year);
			currentDate.setStopMonth(monthOfYear);
			currentDate.setStopDay(dayOfMonth);
			
			updateDisplayStop();
		}
	};

	private void updateDisplayStart() {
		startDateText.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(this.currentDate.getStartMonth() + 1).append("-").append(this.currentDate.getStartDay())
				.append("-").append(this.currentDate.getStartYear()).append(" "));
	}

	private void updateDisplayStop() {
		stopDateText.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(this.currentDate.getStopMonth() + 1).append("-").append(this.currentDate.getStopDay()).append("-")
				.append(this.currentDate.getStopYear()).append(" "));
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID1:
			return new DatePickerDialog(this, mDateSetListenerStart, this.currentDate.getStartYear(),
					this.currentDate.getStartMonth(), this.currentDate.getStartDay());
		case DATE_DIALOG_ID2:
			return new DatePickerDialog(this, mDateSetListenerStop, this.currentDate.getStopYear(),
					this.currentDate.getStopMonth(), this.currentDate.getStopDay());
		}
		return null;
	}
	
	private void InitializeSpinner() {

		Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
		
		ArrayList<String> tags = globalState.GetTags(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tags);	
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tagComboBox.setAdapter(adapter);

        String defaultTag  = globalState.CurrentTag();
        int position = adapter.getPosition(defaultTag);
        
        if (position < 0) {
        	position = 0;
        	tagComboBox.setSelection(position);
        	globalState.SetCurrentTag((String)tagComboBox.getSelectedItem());
        } else {
        	tagComboBox.setSelection(position);
        }

		tagComboBox.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id) {

				GlobalState globalState = (GlobalState) getApplication();
				Spinner tagComboBox = (Spinner) findViewById(R.id.tagComboBox);
				String tag = (String) tagComboBox.getSelectedItem();
				globalState.SetCurrentTag(tag);
				InitializeSpinner();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				globalState.SetCurrentTag("");
			}

		});
	}
}